package ssm.controller;

import com.mysql.cj.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import ssm.myPojo.PerCustom;
import ssm.myPojo.PubCustom;
import ssm.pojo.Per;
import ssm.pojo.Pub;
import ssm.pojo.User;
import ssm.service.UserService;
import ssm.service.impl.UserServiceImpl;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.Enumeration;
import java.util.List;
import java.util.UUID;

@Controller
public class PersonController {

    @Autowired
    private UserService userService = new UserServiceImpl();

    @RequestMapping("/person")
    public String person(Model model,HttpSession session){
        String username = session.getAttribute("username").toString();
        model.addAttribute("username",username);
        return "per/person";
    }

    @RequestMapping("/perSour")
    public String perSour(Model model,
                          @RequestParam(value = "username",required = true) String username)throws Exception{

        System.out.println("收到页面传递过来的username"+username);
        //调用service查询私有资源
        List<PerCustom> perList = userService.findPerByName(username);

        model.addAttribute("sours",perList);
        return "per/show_Sour";
    }

    @RequestMapping("/pubSour")
    public  String pubSour(Model model,
                           @RequestParam(value = "username",required = true) String username)throws Exception{

        //调用service查询私有资源
        List<PubCustom> pubList = userService.findPubByName(username);

        model.addAttribute("sours",pubList);

        return "per/show_Sour";
    }

    //跳转到用户信息更改页面
    @RequestMapping("/perMessageEdit")
    public String perMessageEdit(Model model, HttpSession session)throws Exception{

        //调用service查询用户信息
        User userMessage = userService.findUserById((Integer)session.getAttribute("userId"));
        //将用户信息传入页面并显示
        model.addAttribute("userMessage",userMessage);

        return "per/edit";
    }

    //用户信息提交
    @RequestMapping("/perMessageCommit")
    public String perMessageCommit(Model model,
                                   MultipartFile per_pic,
                                   @Validated User user, BindingResult bindingResult
                                   )throws Exception{
        //判断并输出错误信息
        if(bindingResult.hasErrors()){
            List<ObjectError> objectErrors = bindingResult.getAllErrors();
            //将错误信息输出到控制台
            for(ObjectError error:objectErrors){
                System.out.println(error.getDefaultMessage());
            }
            //将错误信息传入到页面
            model.addAttribute("objectErrors",objectErrors);
        }

        //图片文件的保存
        if(per_pic!=null){
            //选择用于存储图片的目录
            String pic_path = "E:\\1_毕业论文综合\\资源目录\\perPic\\";
            //获取图片名称，并创建新的图片名称
            String filename = per_pic.getOriginalFilename();
            String newFileName = UUID.randomUUID()+filename.substring(filename.lastIndexOf("."));
            //将图片保存到目录中
            File newfile = new java.io.File(pic_path,newFileName);
            if(!filename.equals("")) {
                if (!newfile.getParentFile().exists()) {
                    newfile.getParentFile().mkdirs();//mkdirs()可以创建多及目录，mkdir()只能创建以及目录
                }
                per_pic.transferTo(newfile);//将图片写入到新创建的文件夹中
            }
            //将图片名存入到对象对应的属性中
            user.setPerPic(newFileName);
        }
        //用户信息的更改上传
        userService.updateUser(user);

        return "success";
    }

    //提交成功提示
    @RequestMapping("/success")
    public String success()throws Exception{

        return "";
    }
}
